import 'dart:io';

import 'package:dio/dio.dart';
import 'package:flutter/foundation.dart';

/// 错误处理拦截器
class RequestInterceptor extends InterceptorsWrapper {
  @override
  Future<void> onRequest(
      RequestOptions options, RequestInterceptorHandler handler) async {
    ///这里加入版本信息 在header，可以根据需求更改

    //var token = CookieManager.getCookies(cookies);
    options.headers['platform'] = Platform.operatingSystem;

    ///这里将空值参数去除掉，可根据自己的需求更改
    options.queryParameters.removeWhere((key, value) => value == null);

    String params = "";
    String mark = "&";

    if (!kReleaseMode) {
      options.queryParameters.forEach((k, v) {
        if (v == null) return;
        params = "$params${params.isEmpty ? "" : mark}$k=$v";
      });
    }
    if (options.path.contains("login_android")) {
      if (options.headers.containsKey("cookie")) {
        options.headers.remove("cookie");
      }
    }
    return super.onRequest(options, handler);
  }
}
